package com.fjzx.androidframe.ui.xrecyclerview;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fjzx.androidframe.ui.R;

/**
 * @author xiongyongcong
 */
public class FooterItemDecoration extends RecyclerView.ItemDecoration {
    private View footerView;
    private boolean drawFooter;

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        if (drawFooter) {
            RecyclerView.LayoutManager manager = parent.getLayoutManager();
            if (manager == null || !(manager instanceof LinearLayoutManager)) {
                return;
            }
            LinearLayoutManager linearManager = (LinearLayoutManager) manager;
            int position = linearManager.findLastCompletelyVisibleItemPosition();
            if (position != linearManager.getItemCount() - 1) {
                return;
            }
            if (footerView == null) {
                footerView = initFooterView(parent);
            }
            View child = linearManager.findViewByPosition(position);
            if (child != null) {
                canvas.save();
                canvas.translate(0, child.getBottom());
                footerView.draw(canvas);
                canvas.restore();
            }
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if (manager == null || !(manager instanceof LinearLayoutManager)) {
            return;
        }
        LinearLayoutManager linearManager = (LinearLayoutManager) manager;
        int position = parent.getChildAdapterPosition(view);
        if (position == linearManager.getItemCount() - 1) {
            if (footerView == null) {
                footerView = initFooterView(parent);
            }
            if (drawFooter) {
                outRect.set(0, 0, 0, footerView.getMeasuredHeight());
            }
        }
    }

    private View initFooterView(RecyclerView parent) {
        View footerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.base_layout_footer, parent, false);

        if (footerView.getLayoutParams() == null) {
            footerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
        int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.UNSPECIFIED);
        int childWidth = ViewGroup.getChildMeasureSpec(widthSpec, parent.getPaddingLeft() + parent.getPaddingRight(), footerView.getLayoutParams().width);
        int childHeight = ViewGroup.getChildMeasureSpec(heightSpec, parent.getPaddingTop() + parent.getPaddingBottom(), footerView.getLayoutParams().height);
        footerView.measure(childWidth, childHeight);
        footerView.layout(0, 0, footerView.getMeasuredWidth(), footerView.getMeasuredHeight());
        return footerView;
    }

    public void setDrawFooter(boolean drawFooter) {
        this.drawFooter = drawFooter;
    }
}
